//
//  BaseViewController.m
//  DianBa
//
//  Created by karl on 2018/8/20.
//  Copyright © 2018年 DianBa. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()<MBProgressHUDDelegate>{
    BOOL isAutoCloseProgress;
}

@property (nonatomic,strong) MBProgressHUD *progressHUD;

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setHidden:YES];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setupNavBar];
    self.uiManagement = [DBManagement sharedInstance];
    self.stime = @"";
}

-(void)createRightWithTitle:(NSString*)title{
    [self.customNavBar wr_setRightButtonWithTitle:title titleColor:[UIColor whiteColor]];
}

-(void)createRightWithImg:(NSString*)imgName{
    [self.customNavBar wr_setRightButtonWithImage:[UIImage imageNamed:imgName]];
}

-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

- (void)setExtraCellLineHidden:(UITableView *)tableView{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}

- (UIImage *)imageWithColor:(UIColor *)color{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

#pragma mark -
#pragma mark 页面传值接口
-(void) callbackValue : (id) value fromViewControllerClass : (Class) fromClazz toViewControllerClassName : (NSString *) toClazzName{
    
}

-(void) showProgress{
    if (nil != self.view) {
        self.progressHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.progressHUD.delegate = self;
        self.progressHUD.detailsLabelFont = [UIFont boldSystemFontOfSize:14.0f];
        isAutoCloseProgress = NO;
    }
}

-(void) showProgressWithText : (NSString *) context withDelayTime : (NSUInteger) sec{
    
    if (nil == context || [context isEqualToString:@""]) {
        return;
    }
    
    if (nil == self.progressHUD) {
        [self showProgress];
    }
    
    if (sec <= 1) {
        sec = 2;
    }
    self.progressHUD.mode = MBProgressHUDModeText;
    
    self.progressHUD.detailsLabelText = context;
    //    [self.progressHUD seth]
    
    
    [self.progressHUD hide:YES afterDelay:sec];
}

-(void) showProgressWithText:(NSString *)text{
    [self showProgress];
    self.progressHUD.detailsLabelText = text;
}

-(void) showProgressWithText:(NSString *)text dimBackground:(BOOL)isBackground{
    [self showProgressWithText:text];
    //    self.progressHUD.dimBackground = isBackground;
}

-(void) showProgressAutoCloseInNetwork{
    [self showProgress];
    isAutoCloseProgress = YES;
}

-(void) showProgressAutoCloseInNetwork:(NSString *)text{
    [self showProgressAutoCloseInNetwork];
    self.progressHUD.detailsLabelText = text;
}

-(void) showProgressAutoCloseInNetwork : (NSString *) text dimBackground : (BOOL) isBackground{
    [self showProgressAutoCloseInNetwork:text];
    //    self.progressHUD.dimBackground = isBackground;
}

-(void) autoCloseProgress{
    if (isAutoCloseProgress && nil != self.progressHUD) {
        [self.progressHUD hide:YES];
    }
}

-(void) showProgressAutoWithText : (NSString *) context withDelayTime : (NSUInteger) sec{
    [self showProgressWithText:context withDelayTime:sec];
}

-(void) closeProgress{
    if (nil != self.progressHUD) {
        [self.progressHUD hide:YES];
    }
}

-(void) showWhileExecuting : (SEL) sel withText : (NSString *) text withDetailText : (NSString *) detailText{
    if (nil != self.progressHUD) {
        return;
    }
    self.progressHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.navigationController.view addSubview:self.progressHUD];
    self.progressHUD.delegate = self;
    
    if ([self stringIsNilOrEmpty:text]) {
        self.progressHUD.labelText = text;
    }
    
    if ([self stringIsNilOrEmpty:detailText]) {
        self.progressHUD.detailsLabelText = detailText;
    }
    
    [self.progressHUD showWhileExecuting:sel onTarget:self withObject:nil animated:YES];
}

-(void) showProgressOnwindowsWithText : (NSString *) context withDelayTime : (NSUInteger) sec{
    if (nil == self.progressHUD) {
        if ( [[[UIApplication sharedApplication] windows] count] >=1) {
            UIWindow *tempKeyboardWindow = [[[UIApplication sharedApplication] windows] lastObject];
            self.progressHUD = [[MBProgressHUD alloc] initWithView:tempKeyboardWindow];
            [tempKeyboardWindow addSubview:self.progressHUD];
            [self.progressHUD show:YES];
            self.progressHUD.delegate = self;
            isAutoCloseProgress = NO;
        }
        
        else
        {
            return;
        }
    }
    
    if (sec <= 1) {
        sec = 2;
    }
    self.progressHUD.mode = MBProgressHUDModeText;
    self.progressHUD.detailsLabelText = context;
    [self.progressHUD hide:YES afterDelay:sec];
}

-(void) showProgressOnwindowsWithText : (NSString *) context{
    if (nil == self.progressHUD) {
        if ( [[[UIApplication sharedApplication] windows] count] >=1) {
            UIWindow *tempKeyboardWindow = [[[UIApplication sharedApplication] windows] lastObject];
            self.progressHUD = [[MBProgressHUD alloc] initWithView:tempKeyboardWindow];
            [tempKeyboardWindow addSubview:self.progressHUD];
            [self.progressHUD show:YES];
            self.progressHUD.delegate = self;
            isAutoCloseProgress = NO;
        }else{
            return;
        }
    }
    self.progressHUD.mode = MBProgressHUDModeText;
    self.progressHUD.detailsLabelText = context;
    //    self.progressHUD.dimBackground = YES;
}

#pragma mark -
#pragma mark MBProgressHUDDelegate methods

- (void)hudWasHidden:(MBProgressHUD *)hud {
    [self.progressHUD removeFromSuperview];
    self.progressHUD = nil;
}

#pragma mark -
#pragma mark Other methods
-(BOOL) stringIsNilOrEmpty : (NSString *) str{
    if (nil != str && ![str isEqualToString:@""]) {
        return NO;
    }else{
        return YES;
    }
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    //    [[PalmUIManagement sharedInstance] canelUIHttpRequestFrom:self];
    //    [self clearUINetWorkStatus];
}

#pragma mark - 计算文字高度
- (CGSize)boundingRectWithSize:(CGSize)size withFont:(UIFont*)font text:(NSString*)text{
    NSDictionary *attribute = @{NSFontAttributeName: font};
    
    CGSize retSize ;
    
    
    retSize  = [text boundingRectWithSize:size
                                  options:\
                NSStringDrawingTruncatesLastVisibleLine |
                NSStringDrawingUsesLineFragmentOrigin |
                NSStringDrawingUsesFontLeading
                               attributes:attribute
                                  context:nil].size;
    
    
    CGSize aSize = CGSizeMake(retSize.width, retSize.height);
    
    return aSize;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupNavBar
{
    [self.view addSubview:self.customNavBar];
    WS(vcd);
    // 设置自定义导航栏背景图片
    self.customNavBar.barBackgroundColor = [UIColor colorWithString:MAIN_COLOR];
    self.customNavBar.onClickRightButton = ^{
        [vcd rightAction:nil];
    };
    // 设置自定义导航栏标题颜色
    self.customNavBar.titleLabelColor = [UIColor whiteColor];
    
    if (self.navigationController.childViewControllers.count != 1 && !self.isBackTitle) {
        [self.customNavBar wr_setLeftButtonWithNormal:[UIImage imageNamed:@"back"] highlighted:[UIImage imageNamed:@"back"]];
    }
}

- (WRCustomNavigationBar *)customNavBar
{
    if (_customNavBar == nil) {
        _customNavBar = [WRCustomNavigationBar CustomNavigationBar];
    }
    return _customNavBar;
}

@end
